//
//  CommodityView.m
//  B2C
//
//  Created by xiaowu on 2017/7/26.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "CommodityView.h"

@interface CommodityView ()

@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, strong) UILabel *priceLabel;

@property (nonatomic, strong) UIButton *button;
@end

@implementation CommodityView

+ (CGFloat)getHeightWithWidth:(CGFloat)width {
    CGFloat height = 0;
    height += width;
    // 70
    height += 5;
    height += 18;
    height += 2;
    height += 16;
    height += 2;
    height += 20;
    height += 7;
    return height;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.iconImageView];
        [self addSubview:self.titleLabel];
        [self addSubview:self.subTitleLabel];
        [self addSubview:self.priceLabel];
        [self addSubview:self.button];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.iconImageView.frame = CGRectMake(0, 0, self.width, self.width);
    self.titleLabel.frame = CGRectMake(10, self.iconImageView.bottom + 5, self.width - 20, 18);
    self.subTitleLabel.frame = CGRectMake(10, self.titleLabel.bottom + 2, self.width - 20, 16);
    self.priceLabel.frame = CGRectMake(10, self.subTitleLabel.bottom + 2, self.width - 20, 20);
    self.button.frame = self.bounds;
}

- (void)setItemViewWithImageUrl:(NSString *)url title:(NSString *)title subTitle:(NSString *)subTitle price:(NSString *)price {
//    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:PlaceholderImagePictures];
    
    [self.iconImageView animationWithImageURL:url placeholderImage:PlaceholderImagePictures];
    self.titleLabel.text = title;
    self.subTitleLabel.text = subTitle;
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@", price];
}


#pragma mark - Action
- (void)buttonClick:(UIButton *)sender {
    if (self.block) {
        self.block();
    }
}

#pragma mark - Getters
- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
    }
    return _iconImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = TextNormalFont14;
//        _titleLabel.numberOfLines = 0;
//        [_titleLabel sizeToFit];
    }
    return _titleLabel;
}

- (UILabel *)subTitleLabel {
    if (!_subTitleLabel) {
        _subTitleLabel = [[UILabel alloc] init];
        _subTitleLabel.font = TextNormalFont12;
        _subTitleLabel.textColor = [UIColor gray96Color];
        _subTitleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _subTitleLabel;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = TextNormalFont16;
        _priceLabel.textColor = [UIColor orangePriceColor];
        _priceLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _priceLabel;
}

- (UIButton *)button {
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}



@end
